package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.util.GetUidUtil;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.net.HttpCookie;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName: OrderController
 * @Author: WangEnzhao
 * @Description: 订单
 * @CreateTime: 2021/12/20:55:00
 */
@RestController
@RequestMapping("shopping")
public class OrderController {
    @Reference
    OrderCoreService orderCoreService;

    @Reference
    OrderQueryService orderQueryService;

    /***
    * @Author: WangEnzhao
    * @Description: 创建订单
    * @CreateTime: 09:45 12/14/2021
    * @Param: [request, servletRequest]
    * @return: com.mall.commons.result.ResponseData
    */
    @PostMapping("order")
    public ResponseData addOrder(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest){
        Long uid = GetUidUtil.getUid(servletRequest);

        //设置uniqueKey
        String uniqueKey = UUID.randomUUID()+"";
        uniqueKey = uniqueKey.replaceAll("-","");
        request.setUniqueKey(uniqueKey);
        request.setUserId(uid);
        CreateOrderResponse response = orderCoreService.createOrder(request);
        if(response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /***
    * @Author: WangEnzhao
    * @Description: 获取所有订单
    * @CreateTime: 09:45 12/14/2021
    * @Param: [request, servletRequest]
    * @return: com.mall.commons.result.ResponseData
    */
    @GetMapping("order")
    public ResponseData queryData(OrderListRequest request, HttpServletRequest servletRequest){
        Long uid = GetUidUtil.getUid(servletRequest);
        request.setUserId(uid);
        OrderListResponse response = orderQueryService.orderList(request);
        if(response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /***
    * @Author: WangEnzhao
    * @Description: 获取订单详情
    * @CreateTime: 09:44 12/14/2021
    * @Param: [id, servletRequest]
    * @return: com.mall.commons.result.ResponseData
    */
    @GetMapping("order/{id}")
    public ResponseData orderDetail(@PathVariable("id") String id, HttpServletRequest servletRequest ){
        OrderDetailRequest request = new OrderDetailRequest();
        request.setOrderId(id);
        OrderDetailResponse1 response = orderQueryService.orderDetail(request);
        if(response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /***
    * @Author: WangEnzhao
    * @Description: 订单取消
    * @CreateTime: 09:44 12/14/2021
    * @Param: [id, servletRequest]
    * @return: com.mall.commons.result.ResponseData
    */
    @PostMapping("cancelOrder")
    public ResponseData orderCancel(@RequestBody() Map<String,String> map, HttpServletRequest servletRequest){

        CancelOrderRequest request = new CancelOrderRequest();
        request.setOrderId(map.get("orderId"));
        CancelOrderResponse response = orderQueryService.orderCancel(request);
        if(response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /***
    * @Author: WangEnzhao
    * @Description: 订单删除
    * @CreateTime: 09:44 12/14/2021
    * @Param: [servletRequest, id]
    * @return: com.mall.commons.result.ResponseData
    */
    @DeleteMapping("order/{id}")
    public ResponseData orderDelete(HttpServletRequest servletRequest, @PathVariable("id") String id){
        DeleteOrderRequest request = new DeleteOrderRequest();
        request.setOrderId(id);
        DeleteOrderResponse response = orderQueryService.deleteOrder(request);
        if(response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }
}
